﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Express.Common;
using Express.DAL;

namespace Express
{
    /// <summary>
    /// 程序登录窗体
    /// </summary>
    public partial class FormLogin : Form
    {
        /// <summary>
        /// 公共类对象
        /// </summary>
        private readonly CommClass cc = new CommClass();

        /// <summary>
        /// 数据库帮助类对象
        /// </summary>
        private readonly DataOperate dataOper = new DataOperate();

        /// <summary>
        /// 构造函数
        /// </summary>
        public FormLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 登录用户文本框按下键盘Enter键后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtCode_KeyDown(object sender, KeyEventArgs e)
        {
            cc.SetFocus(e, TxtPwd);
        }

        /// <summary>
        /// 登录密码文本框按下键盘Enter键后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PicLogin_Click(sender, e);  //把e传递给EventArgs是多态的特性
            }
        }

        /// <summary>
        /// 单击登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PicLogin_Click(object sender, EventArgs e)
        {
            SqlDataReader sdr = null;
            if (String.IsNullOrEmpty(TxtCode.Text.Trim()))
            {
                MessageBox.Show("登录用户不许为空！", "软件提示");
                TxtCode.Focus();
                return;
            }
            if (String.IsNullOrEmpty(TxtPwd.Text))
            {
                MessageBox.Show("登录密码不许为空！", "软件提示");
                TxtPwd.Focus();
                return;
            }
            //用户编码不重复
            string strSql = "select * from tb_Operator where OperatorCode = '" + TxtCode.Text.Trim() + "'";
            try
            {
                sdr = dataOper.GetDataReader(strSql);
                if (!sdr.HasRows)  //若该用户编码无数据记录
                {
                    MessageBox.Show("登录用户不正确！", "软件提示");
                    TxtCode.Focus();
                }
                else
                {
                    sdr.Read(); //读取唯一的一行记录
                    if (!(MD5Encrypt.GetMD5Password(TxtPwd.Text) == sdr["Password"].ToString()))  //若密码不相同
                    {
                        MessageBox.Show("登录密码不正确！", "软件提示");
                        TxtPwd.Focus();
                    }
                    else
                    {
                        GlobalProperty.OperatorCode = sdr["OperatorCode"].ToString();
                        GlobalProperty.OperatorName = sdr["OperatorName"].ToString();
                        GlobalProperty.Password = sdr["Password"].ToString();
                        GlobalProperty.IsFlag = sdr["IsFlag"].ToString();
                        this.Hide();
                        AppForm formApp = new AppForm();
                        formApp.Show();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
            }
            finally
            {
                sdr.Close();
            }
        }

        /// <summary>
        /// 单击重置按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PicReset_Click(object sender, EventArgs e)
        {
            TxtCode.Text = string.Empty;
            TxtPwd.Text = string.Empty;
        }

        /// <summary>
        /// 单击退出按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PicQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
